package org.csu.mypetstore.web.servlets;

import com.sun.org.apache.xpath.internal.operations.Or;
import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Cart;
import org.csu.mypetstore.domain.Order;
import org.csu.mypetstore.service.LogService;
import org.csu.mypetstore.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class NewOrderServlet extends HttpServlet {

    private static final String CONFIRM_ORDER_FORM = "/WEB-INF/jsp/order/ConfirmOrder.jsp";
    private static final String SHIPPING_FORM = "/WEB-INF/jsp/order/ShippingForm.jsp";
    private static final String VIEW_ORDER = "/WEB-INF/jsp/order/ViewOrder.jsp";
    private static final String ERROR = "/WEB-INF/jsp/common/Error.jsp";

    private Order order;
    boolean shippingAddressRequired;
    boolean confirmed;
    List<Order> orderList;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        System.out.println("已经进入");
        HttpSession session = request.getSession();

        shippingAddressRequired = (Boolean) session.getAttribute("shippingAddressRequired");
        confirmed = (Boolean) session.getAttribute("confirmed");
//        confirmed = true;
        session.setAttribute("confirmed", confirmed);
        order = (Order) session.getAttribute("order");


//        System.out.println(request.getParameter("onClickConfirmed"));
//        confirmed = Boolean.getBoolean(request.getParameter("onClickConfirmed"));
//        System.out.println(confirmed);
//        System.out.println(request.getParameter("onClickConfirmed"));
        if (request.getParameter("onClickConfirmed") != null) {
            confirmed = true;
            session.setAttribute("confirmed", confirmed);
        }

        if (request.getParameter("shippingAddressRequired") != null) {
            shippingAddressRequired = true;
            session.setAttribute("shippingAddressRequired", shippingAddressRequired);
        }



        //对order赋值
        String cardType = request.getParameter("cardType");
        String creditCard = request.getParameter("creditCard");
        String expiryDate = request.getParameter("expiryDate");
        String billToFirstName = request.getParameter("billToFirstName");
        String billToLastName = request.getParameter("billToLastName");
        String billAddress1 = request.getParameter("billAddress1");
        String billAddress2 = request.getParameter("billAddress2");
        String billCity = request.getParameter("billCity");
        String billState = request.getParameter("billState");
        String billZip = request.getParameter("billZip");
        String billCountry = request.getParameter("billCountry");
        String shipToFirstName = request.getParameter("shipToFirstName");
        String shipToLastName = request.getParameter("shipToLastName");
        String shipAddress1 = request.getParameter("shipAddress1");
        String shipAddress2 = request.getParameter("shipAddress2");
        String shipCity = request.getParameter("shipCity");
        String shipState = request.getParameter("shipState");
        String shipZip = request.getParameter("shipZip");
        String shipCountry = request.getParameter("shipCountry");

        if (!shippingAddressRequired && !confirmed) {

            if (cardType != null) {
                order.setCardType(cardType);
            }
            if (creditCard != null) {
                order.setCreditCard(creditCard);
            }
            if (expiryDate != null) {
                order.setExpiryDate(expiryDate);
            }

            if (billToFirstName != null) {
                order.setBillToFirstName(billToFirstName);
            }
            if (billToLastName != null) {
                order.setBillToLastName(billToLastName);
            }
            if (billAddress1 != null) {
                order.setBillAddress1(billAddress1);
            }
            if (billAddress2 != null) {
                order.setBillAddress2(billAddress2);
            }
            if (billCity != null) {
                order.setBillCity(billCity);
            }
            if (billState != null) {
                order.setBillState(billState);
            }
            if (billZip != null) {
                order.setBillZip(billZip);
            }
            if (billCountry != null) {
                order.setBillCountry(billCountry);
            }

            if (shipToFirstName != null) {
                order.setShipToFirstName(shipToFirstName);
            } else {
                order.setShipToFirstName(billToFirstName);
            }
            if (shipToLastName != null) {
                order.setShipToLastName(shipToLastName);
            } else {
                order.setShipToFirstName(billToLastName);
            }
            if (shipAddress1 != null) {
                order.setShipAddress1(shipAddress1);
            } else {
                order.setShipAddress1(billAddress1);
            }
            if (shipAddress2 != null) {
                order.setShipAddress2(shipAddress2);
            } else {
                order.setShipAddress2(billAddress2);
            }
            if (shipCity != null) {
                order.setShipCity(shipCity);
            } else {
                order.setShipCity(billCity);
            }
            if (shipState != null) {
                order.setShipState(shipState);
            } else {
                order.setShipState(billState);
            }
            if (shipZip != null) {
                order.setShipZip(shipZip);
            } else {
                order.setShipZip(billZip);
            }
            if (shipCountry != null) {
                order.setShipCountry(shipCountry);
            } else {
                order.setShipCountry(billCountry);
            }
        }

        session.setAttribute("order", order);


        if (shippingAddressRequired) {
            shippingAddressRequired = false;
            session.setAttribute("shippingAddressRequired", shippingAddressRequired);
//            return new ForwardResolution(SHIPPING);

            Account account = (Account) session.getAttribute("account");
            if(account != null){
                HttpServletRequest httpRequest= request;
                String strBackUrl = "http://" + request.getServerName() + ":" + request.getServerPort()
                        + httpRequest.getContextPath() + httpRequest.getServletPath() + "?";

                LogService logService = new LogService();
                String logInfo = logService.logInfo(" ") + strBackUrl + SHIPPING_FORM;
                logService.insertLogInfo(account.getUsername(), logInfo);
            }
            request.getRequestDispatcher(SHIPPING_FORM).forward(request, response);
        } else if (!confirmed) {
//            confirmed = false;
//            session.setAttribute("confirmed", confirmed);
//            return new ForwardResolution(CONFIRM_ORDER);
            if (shippingAddressRequired == true) {
                shippingAddressRequired = false;
                session.setAttribute("shippingAddressRequired", shippingAddressRequired);
            }


            Account account = (Account) session.getAttribute("account");
            if(account != null){
                HttpServletRequest httpRequest= request;
                String strBackUrl = "http://" + request.getServerName() + ":" + request.getServerPort()
                        + httpRequest.getContextPath() + httpRequest.getServletPath() + "?";

                LogService logService = new LogService();
                String logInfo = logService.logInfo(" ") + strBackUrl + CONFIRM_ORDER_FORM;
                logService.insertLogInfo(account.getUsername(), logInfo);
            }
            request.getRequestDispatcher(CONFIRM_ORDER_FORM).forward(request, response);
        } else if (order != null) {
            OrderService orderService = new OrderService();

            //插入订单
            orderService.insertOrder(order);

//            CartActionBean cartBean = (CartActionBean) session.getAttribute("/actions/Cart.action");
//            cartBean.clear();

            //清空购物车
            String userid = (String) session.getAttribute("username");
            Cart cart = new Cart();
            cart.removeAllCartItemsByUserid(userid);
            String workingItemId = null;
            session.setAttribute("cart", cart);
            session.setAttribute("workingItemId", workingItemId);
//            session.setAttribute("username", null);

            String message = "Thank you, your order has been submitted.";
            session.setAttribute("message", message);

//            return new ForwardResolution(VIEW_ORDER);
            Account account = (Account) session.getAttribute("account");
            if(account != null){
                HttpServletRequest httpRequest= request;
                String strBackUrl = "http://" + request.getServerName() + ":" + request.getServerPort()
                        + httpRequest.getContextPath() + httpRequest.getServletPath() + "?";

                LogService logService = new LogService();
                String logInfo = logService.logInfo(" ") + strBackUrl + VIEW_ORDER;
                logService.insertLogInfo(account.getUsername(), logInfo);
            }
            request.getRequestDispatcher(VIEW_ORDER).forward(request, response);
        } else {
            String message = "An error occurred processing your order (order was null).";
            session.setAttribute("message", message);

//            return new ForwardResolution(ERROR);
            Account account = (Account) session.getAttribute("account");
            if(account != null){
                HttpServletRequest httpRequest= request;
                String strBackUrl = "http://" + request.getServerName() + ":" + request.getServerPort()
                        + httpRequest.getContextPath() + httpRequest.getServletPath() + "?";

                LogService logService = new LogService();
                String logInfo = logService.logInfo(" ") + strBackUrl + ERROR;
                logService.insertLogInfo(account.getUsername(), logInfo);
            }
            request.getRequestDispatcher(ERROR).forward(request, response);
        }
    }
}
